package homework;

import java.io.*;

public class testStream {
    /**
     * 1. 写一个方法，实现文件的拷贝
     *
     * @param org  源路径
     * @param dest 终点路径
     */
    public void fileCopy(String org, String dest) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(org);
            fos = new FileOutputStream(dest);
            int d = 0;
            while ((d = fis.read()) != -1) {
                fos.write(d);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null && fos != null) {
                try {
                    fis.close();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 2. 输入一段字符串，将该字符串追加到指定文本文件中
     * @param name 文件路径
     * @param content 追加类容
     */
    public void appendFile(String name, String content) {
        Writer fos = null;

        try {
            fos = new FileWriter(name,true);
            fos.append(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {
        testStream test = new testStream();
//        test.fileCopy("C:/Users/76618/Desktop/Java/testFile.txt", "C:/Users/76618/Desktop/Java/copy/testFile.txt");
//        test.fileCopy("C:/Users/76618/Desktop/images.7z", "C:/Users/76618/Desktop/Java/copy/images.7z");
//        test.fileCopy("C:/Users/76618/Desktop/666.jpg", "C:/Users/76618/Desktop/Java/copy/666.jpg");

        test.appendFile("C:/Users/76618/Desktop/Java/testFile.txt", "666");


    }
}
